package com.life.statistics;

import java.util.ArrayList;

import com.math.ArrayMath;

public class StatisticsElement
{
    protected boolean calculated = false;
    protected double sum = 0;
    protected double mean = 0;
    protected double standardDeviation=0;
    protected ArrayList<Double>data = new ArrayList<Double>();
     
    int boardX = -1;
    int boardY = -1;
    
    public StatisticsElement(int i, int j)
    {
	boardX = i;
	boardY = j;
    }
    
    public double getStdDev()
    {
	if(!calculated)
	{
	    calculate();
	}
	return standardDeviation;
    }
    
    public double getMean()
    {
	if(!calculated)
	{
	    calculate();
	}
	return mean;
    }
    
    public void add(Double value)
    {
	calculated = false;
	data.add(value);
    }
    
    public double getSum()
    {
	if(!calculated)
	{
	    calculate();
	}
	
	return sum;
    }
    
    public double getSumSquared()
    {
	if(!calculated)
	{
	    calculate();
	}
	return 0;
    }
  
    public void calculate() 
    {
	double[] result = ArrayMath.toDoubleArray(data.toArray());
	sum = ArrayMath.mass(result);  
	mean = ArrayMath.mean(result);
	standardDeviation = ArrayMath.standardDeviation(result);
	calculated = true;
    }
    
    public String toString()
    {
	if(!calculated)
	{
	    calculate();
	}
	return "sum: " + sum + "|mean: " + mean + "|stdDev: " + standardDeviation;
    }

    public int getBoardX()
    {
        return boardX;
    }

    public int getBoardY()
    {
        return boardY;
    }
}
